import {
  ArgumentsHost,
  Catch,
  ExceptionFilter,
  HttpException,
} from '@nestjs/common';

import { Request, Response } from 'express';
@Catch()
export class ErrorFilter<T> implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const request = ctx.getRequest<Request>();
    const response = ctx.getResponse<Response>();
    let status = 400;
    let error = {};
    console.log(exception);
    if (exception instanceof HttpException) {
      status = exception.getStatus();
      error = exception.getResponse();
    }

    response.status(status).json({
      code: status,
      msg: error['message'],
      path: request.url,
    });
  }
}
